﻿using ClinicSystem.configure;
using ClinicSystem.FormPage;
using Org.BouncyCastle.Asn1.Cmp;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ClinicSystem
{
	public partial class MainForm : Form
	{
		private Timer statusTimer = new Timer();
		private bool isLoggedIn = false;
		public WndNewMedical medicalEntry = new WndNewMedical();
		public WndQueryInfo prescription = new WndQueryInfo();
		public WndDrugInfo drugInfo = new WndDrugInfo();

		private Dictionary<string, Form> formDictionary = new Dictionary<string, Form>();
		public MainForm()
		{
			Load_OutFile.ConnectionDB("System");
			InitializeComponent();
			InitializeStatusTimer();
			flowLayoutPanel1.Enabled = false;
			button_newDiagnose.Click += buttonswithpage;
			button_prescription.Click += buttonswithpage;
			button_drugInfo.Click += buttonswithpage;
			showPage(tableLayoutPanel1, 0, 1, medicalEntry);
			button_newDiagnose.BackColor = Color.SkyBlue;
		}


		private void InitializeStatusTimer()
		{
			// 创建定时器
			statusTimer = new Timer
			{
				Interval = 1000 // 每秒检查一次
			};
			statusTimer.Tick += StatusTimer_Tick; // 定时器事件
			statusTimer.Start(); // 启动定时器
		}

		private void StatusTimer_Tick(object sender, EventArgs e)
		{
			// 检查登录状态并更新界面
			UpdateLoginStatus();

		}
		private bool loginPending = false;
		private void UpdateLoginStatus()
		{
			if (isLoggedIn)
			{
				label_Hello.Text = "你好！" + GlobalVar.currentUser;
				tableLayoutPanel1.GetControlFromPosition(0, 1).Enabled = true;
				flowLayoutPanel1.Enabled = true;

			}
			else
			{
				label_Hello.Text = "请先登录";
				if (tableLayoutPanel1.GetControlFromPosition(0, 1) != null)
					tableLayoutPanel1.GetControlFromPosition(0, 1).Enabled = false;

				flowLayoutPanel1.Enabled = false;

				if (!loginPending)
				{
					loginPending = true;
					ShowLoginForm();
				}

			}
		}

		private void ShowLoginForm()
		{
			// 使用BeginInvoke确保在UI线程上执行
			this.BeginInvoke((MethodInvoker)delegate
			{
				using (Login loginform = new Login())
				{
					if (loginform.ShowDialog() == DialogResult.OK)
					{
						isLoggedIn = true;
						// 更新登录按钮文本
						button_Login.Text = "退  出";
					}
					//loginPending = false;
				}
			});
		}

		public void showPage(TableLayoutPanel tableLayoutPanel, int row, int col, Form userform)
		{
			if (userform == null) return;
			userform.TopLevel = false;
			if (formDictionary.TryGetValue(userform.Name, out Form form))//检测窗口是否添加进来
			{
				if (form.IsDisposed)//如果窗体已经被释放，则从字典中移除再创建一个新的实例
				{
					formDictionary.Remove(userform.Name);
					showPage(tableLayoutPanel, row, col, userform);
					return;

				}
			}
			else
			{
				formDictionary.Add(userform.Name, userform);
			}

			var controlsToRemove = new List<Control>();
			foreach (Control control in tableLayoutPanel.Controls)
			{
				if (tableLayoutPanel.GetRow(control) == row &&
					tableLayoutPanel.GetColumn(control) == col)
				{
					controlsToRemove.Add(control);
				}
			}
			foreach (var control in controlsToRemove)
			{
				tableLayoutPanel.Controls.Remove(control);
			}

			// 添加新窗体
			tableLayoutPanel.Controls.Add(userform, col, row);
			userform.Dock = DockStyle.Fill;

			// 确保窗体可见
			if (!userform.Visible)
			{
				userform.Show();
			}
			userform.BringToFront();

		}

		private void buttonswithpage(object obj, EventArgs e)
		{
			Button button = (Button)obj;
			int Value = Convert.ToInt16(button.Tag);
			foreach (Control control in flowLayoutPanel1.Controls)
			{
				if (control is Button)
				{
					control.BackColor = Color.White;

				}
			}
			button.BackColor = Color.SkyBlue;

			if (Value == 1)
			{
				showPage(tableLayoutPanel1, 0, 1, medicalEntry);
			}
			else if (Value == 2)
			{
				showPage(tableLayoutPanel1, 0, 1, prescription);
			}
			else if (Value == 3)
			{
				showPage(tableLayoutPanel1, 0, 1, drugInfo);
			}
		}

		private void button1_Click(object sender, EventArgs e)
		{
			if (!isLoggedIn)
			{
				Login loginform = new Login();
				DialogResult result = loginform.ShowDialog();
				if (result == DialogResult.OK)
				{
					this.button_Login.Text = "退  出";
					isLoggedIn = true;

				}
			}
			else
			{
				GlobalVar.currentUser = "";
				this.button_Login.Text = "登 录";
				isLoggedIn = false;
			}

		}

		private void button_print_Click(object sender, EventArgs e)
		{

		}



		private void MainForm_Load(object sender, EventArgs e)
		{
			//Load_OutFile loader = new Load_OutFile();

			List<string> nametype = new List<string>
			{
				"name VARCHAR(20)","gender VARCHAR(5)","age INT","medicalID VARCHAR(20)","telnumber VARCHAR(20)","address VARCHAR(25)"
			};
			List<string> colhead = new List<string>
			{
				"name VARCHAR(20)","doasge INT"
			};
			Load_OutFile.CreatTable("person", nametype);
			Load_OutFile.CreatTable("medicineList", colhead);
			List<string> values = new List<string>
			{
				"里斯","男","22","20251122","136595645","北京"
			};
			//Load_OutFile.InsertData("person",nametype, values);

		}


	}
}
